package com.example.handle.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/21 15:40
 */
@ControllerAdvice(basePackages = {"com.example.controller"})
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public Object  handlerRuntimeExcepetion(HandlerMethod handlerMethod,Exception exception) {
        ModelAndView modelAndView=new ModelAndView();
        int status = HttpStatus.BAD_REQUEST.value();
        log.error(exception.getMessage(), exception);
        modelAndView.setViewName("globalErr");
        modelAndView.addObject("code", status);
        modelAndView.addObject("message", exception.getMessage());
        modelAndView.addObject("className", handlerMethod.getBean().getClass());
        modelAndView.addObject("methodName",  handlerMethod.getMethod().getName());
        log.error("{} >> {} >> {} >> methodParameters{}",handlerMethod.getBean().getClass(),handlerMethod.getMethod().getName(),exception.getMessage(),handlerMethod.getMethodParameters().toString());
        return modelAndView;
    }
}
